package de.lmu.ifi.dbs.trafficmining.ui.nodelist;

import de.lmu.ifi.dbs.trafficmining.graph.Node;
import de.lmu.ifi.dbs.utilities.Collections2;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.event.ListDataListener;

public class NodeListPanel extends javax.swing.JPanel {

    static final Logger log = Logger.getLogger(NodeListPanel.class.getName());
    private final DefaultListModel<Node> listModel = new DefaultListModel<>();

    public NodeListPanel() {
        initComponents();

        removeItem.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (Object v : list.getSelectedValuesList()) {
                    listModel.removeElement(v);
                }
            }
        });
        removeItem.setText("remove");
        
        
    }

    public void clear() {
        listModel.clear();
    }

    public List<Node> getNodes() {
        return Collections2.asList(listModel.elements());
    }

    public void addNode(Node n) {
        if (n == null) {
            log.fine("node = null, ignoring");
            return;
        }
        listModel.addElement(n);
    }

    public void addListDataListener(ListDataListener l) {
        listModel.addListDataListener(l);
    }

    private void maybeRemoveEntry(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() >= 2) {
            for (int i = 0; i < listModel.getSize(); i++) {
                Rectangle cellBounds = list.getCellBounds(i, i);
                if (cellBounds.contains(evt.getPoint())) {
                    listModel.remove(i);
                    return;
                }
            }
        }
    }

    private void maybeRemoveSelected(KeyEvent evt) {
        if (evt.getKeyChar() == KeyEvent.VK_DELETE) {
            for (Object o : list.getSelectedValuesList()) {
                listModel.removeElement(o);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupMenu = new javax.swing.JPopupMenu();
        removeItem = new javax.swing.JMenuItem();
        renderer = new de.lmu.ifi.dbs.trafficmining.ui.nodelist.NodeListCellRenderer();
        javax.swing.JScrollPane scrollpane = new javax.swing.JScrollPane();
        list = new javax.swing.JList();

        removeItem.setText("remove");
        popupMenu.add(removeItem);

        renderer.setText("nodeListCellRenderer1");

        setLayout(new java.awt.BorderLayout());

        list.setModel(listModel);
        list.setCellRenderer(renderer);
        list.setComponentPopupMenu(popupMenu);
        list.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        list.setDragEnabled(true);
        list.setVisibleRowCount(3);
        list.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listMouseClicked(evt);
            }
        });
        list.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                listKeyTyped(evt);
            }
        });
        scrollpane.setViewportView(list);

        add(scrollpane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void listMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseClicked
        maybeRemoveEntry(evt);
    }//GEN-LAST:event_listMouseClicked

    private void listKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_listKeyTyped
        maybeRemoveSelected(evt);
    }//GEN-LAST:event_listKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList list;
    private javax.swing.JPopupMenu popupMenu;
    private javax.swing.JMenuItem removeItem;
    private de.lmu.ifi.dbs.trafficmining.ui.nodelist.NodeListCellRenderer renderer;
    // End of variables declaration//GEN-END:variables
}
